function [nchan] = analysis2xls

%  Created 9/8/03 by B. Ross
%  This funcion will export analysis data out of the Project structure and
%  write it to a Microsoft Excel spreadsheet file.  The function uses the xlswrite.m
%  function that was downloaded from the mathworks website.
%
%  On 9/9/03 updated Excel filename selection logic to allow user to more easily
%  select the path as well as the filename.
%
%  On 10/1/03 fixed problems with data and temp variables when the data is real.
%
%  On 5/24/04 added option to select format of exported complex data, Real/Imag or
%  Mag/Phase.
%
%  On 4/11/05 added logic to check for data type and write the appropriate
%  header line.
%
%  Last modified 4/11/05 by B. Ross
%
%  [number_of_channels_written] = analysis2xls
%
% ********************************************************************************

global Project;

nchan = 0;

%  Check for existance of analysis data in current Project

content = fieldnames(Project.Test_Item.Run.Channel(1));
if strcmp(content{size(content,1)},'Analysis')

	%  Prompt for Analysis Number to Export
	
	[anal_num, varargout] = analysis_prompt;
	
	%  Select Channels to Export
	
	[fchan, lchan, varargout] = channel_select;
    nchan = lchan-fchan+1;

    %  Generate the ordinate & abscissa matrices
    
    for ii = fchan:lchan
        ordinate(:,ii-fchan+1) = Project.Test_Item.Run.Channel(ii).Analysis(anal_num).Ord.';
    end
    abscissa = Project.Test_Item.Run.Channel(fchan).Analysis(anal_num).Abs.';
    
    %  Generate the vector of column headers
    
    for kk = fchan:lchan
        respnam{1,kk-fchan+2} = [Project.Test_Item.Run.Channel(kk).Coordinate ...
                               Project.Test_Item.Run.Channel(kk).Direction ...
                               Project.Test_Item.Run.Channel(kk).Orientation];
    end
    if Project.Test_Item.Run.Channel(fchan).Analysis(anal_num).Type == 1
        respnam{1,1} = 'Time';
	else
        respnam{1,1} = 'Frequency';
	end

    %  Check for complex data
    %  Adjust header for complex data
    if ~isreal(ordinate)
        temp{1} = respnam{1,1};
        for ii = 1:nchan
            temp{2*ii} = respnam{1,ii+1};
            temp{(2*ii)+1} = '';
        end
        respnam = temp;

        % Prompt for real/imag or mag/phase
        [format,status] =comp_data_format_sel;
        
        %  Check to see if user has cancelled operation
		if strcmp(status,'cancel')
            return
		end

        if format == 1 %real/imaginary
            for jj = 1:nchan
                data(:,(2*jj)-1) = real(ordinate(:,jj));
                data(:,(2*jj)) = imag(ordinate(:,jj));
            end
        else %magnitude/phase
            for kk = 1:nchan
                data(:,(2*kk)-1) = abs(ordinate(:,kk));
                data(:,(2*kk)) = angle(ordinate(:,kk))*180/pi;
            end
        end
        ordinate = data;
	end %if ~isreal(ordinate)

	%  Combine abscissa and ordinate data into a single matrix
	
    m = [abscissa,ordinate];
    
    % Set header line based on data type
    
    switch Project.Test_Item.Run.Channel(fchan).Analysis(anal_num).Type
        case {0 1} % Time History
            head = ['Time, Magnitude Channel 1, Magnitude Channel 2, ...'];
        case 2 % PSD
            head = ['Frequency, PSD Channel 1, PSD Channel 2, ...'];
        case 3 % FRF
            if format == 1
                head = ['Frequency, FRF Real Part Channel 1, FRF Imaginary Part Channel 1, '...
                        'FRF Real Part Channel 2, FRF Imaginary Part Channel 2, ...'];
            else
                head = ['Frequency, FRF Magnitude Channel 1, FRF Phase (deg) Channel 1, '...
                        'FRF Magnitude Channel 2, FRF Phase (deg) Channel 2, ...'];
            end
        case 4 % Swept Sine: Magnitude vs. Frequency
            if format == 1
                head = ['Frequency, Real Part Channel 1, Imaginary Part Channel 1, '...
                        'Real Part Channel 2, Imaginary Part Channel 2, ...'];
            else
                head = ['Frequency, Magnitude Channel 1, Phase (deg) Channel 1, '...
                        'Magnitude Channel 2, Phase (deg) Channel 2, ...'];
            end
        case 5 % Swept Sine:FRF
            if format == 1
                head = ['Frequency, FRF Real Part Channel 1, FRF Imaginary Part Channel 1, '...
                        'FRF Real Part Channel 2, FRF Imaginary Part Channel 2, ...'];
            else
                head = ['Frequency, FRF Magnitude Channel 1, FRF Phase (deg) Channel 1, '...
                        'FRF Magnitude Channel 2, FRF Phase (deg) Channel 2, ...'];
            end
        case 6 % SRS
            head = ['Frequency, SRS Magnitude Channel 1, SRS Magnitude Channel 2, ...'];
        case 7 % One-Third Octave
            head = ['Frequency, Magnitude Channel 1, Magnitude Channel 2, ...'];
    end

    %  Prompt for Excel filename and directory using the Project
    %  Setup Filename as the default name.
	
    [fname, pname] = uiputfile([Project.Test_Item.Run.Setup_File '.xls'],...
                        'Select Export File Name');
	if isequal([fname, pname], [0,0])
	    return
	end
    filename = fullfile(pname, fname);

	%  Generate the file
	
    xlswrite(m,head,respnam,filename);
    
else
    analyses_missing_error;
end
